<template>
  <div class="login-page">
    <div class="login-container">
      <!-- Logo区域 -->
      <div class="logo-section">
        <img src="@/assets/images/home.svg" alt="校园生活圈" class="logo" />
        <h1 class="title">校园生活圈</h1>
        <p class="subtitle">连接校园，分享生活</p>
      </div>

      <!-- 登录表单 -->
      <div class="form-section">
        <van-form @submit="handleLogin">
          <van-cell-group inset>
            <van-field
              v-model="loginForm.mobile"
              name="mobile"
              label="手机号"
              placeholder="请输入手机号"
              :rules="[{ required: true, message: '请输入手机号' }]"
              type="tel"
              maxlength="11"
            />
            <van-field
              v-model="loginForm.password"
              type="password"
              name="password"
              label="密码"
              placeholder="请输入密码"
              :rules="[{ required: true, message: '请输入密码' }]"
            />
          </van-cell-group>

          <div class="login-actions">
            <van-button 
              round 
              block 
              type="primary" 
              native-type="submit"
              :loading="loading"
              class="login-btn"
            >
              {{ loading ? '登录中...' : '登录' }}
            </van-button>
          </div>
        </van-form>

        <!-- 其他操作 -->
        <div class="other-actions">
          <div class="links">
            <span @click="goToRegister">还没有账号？立即注册</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { showToast } from 'vant'
import { userApi, type LoginRequest } from '@/api/user'
import { useUserStore } from '@/stores/user'
import { useAppStore } from '@/stores/app'

const router = useRouter()
const userStore = useUserStore()
const appStore = useAppStore()

// 设置页面标题
appStore.setTitle('登录')

// 响应式数据
const loading = ref(false)
const loginForm = reactive<LoginRequest>({
  mobile: '',
  password: ''
})

// 处理登录
const handleLogin = async () => {
  if (loading.value) return
  
  loading.value = true
  try {
    const response = await userApi.login(loginForm)
    
    // 根据实际响应，成功状态码是 1
    if (response.code === 1) {
      // token在 response.data.token 中
      const token = response.data?.token
      const userInfo = {
        id: response.data?.id,
        openid: response.data?.openid,
        token: token
      }
      
      if (token) {
        localStorage.setItem('token', token)
        userStore.setUserInfo(userInfo)
        userStore.setLoginStatus(true)
        
        showToast('登录成功')
        
        // 跳转回首页或之前的页面
        const redirect = router.currentRoute.value.query.redirect as string
        router.replace(redirect || '/')
      } else {
        showToast('登录响应中未找到token')
      }
    } else {
      showToast(response.msg || '登录失败')
    }
  } catch (error: any) {
    console.error('登录失败:', error)
    showToast(error.message || '登录失败，请重试')
  } finally {
    loading.value = false
  }
}

// 跳转到注册页面
const goToRegister = () => {
  router.push('/register')
}
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-container {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: 16px;
  padding: 40px 30px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

.logo-section {
  text-align: center;
  margin-bottom: 40px;
}

.logo {
  width: 80px;
  height: 80px;
  margin-bottom: 16px;
}

.title {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  margin: 0 0 8px 0;
}

.subtitle {
  font-size: 14px;
  color: #666;
  margin: 0;
}

.form-section {
  width: 100%;
}

.login-actions {
  margin-top: 24px;
}

.login-btn {
  height: 48px;
  font-size: 16px;
  font-weight: 500;
}

.other-actions {
  margin-top: 24px;
  text-align: center;
}

.links span {
  color: #667eea;
  font-size: 14px;
  cursor: pointer;
  text-decoration: none;
}

.links span:hover {
  text-decoration: underline;
}
</style>